package com.service.business.base.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import com.service.business.base.service.ConfigService;
import com.service.dao.constants.ConfigConstants;
import com.service.business.base.controller.rep.ConfigRep;
import com.service.business.base.dto.MeasureOfCapitalConfig;
import com.service.dao.enums.CommonStatusEnums;
import com.service.dao.mapper.SysConfigMapper;
import com.service.dao.model.SysConfig;
import com.service.utility.modelmapper.ModelMapperUtil;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.ObjectUtils;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * <dl>
 * <dt>platform</dt>
 * <dd>Description:</dd>
 * <dd>Copyright: Copyright (C) 2018</dd>
 * <dd>Company: </dd>
 * <dd>CreateDate: 2019-04-06</dd>
 * </dl>
 */
@Slf4j
@Service
public class ConfigServiceImpl extends ServiceImpl<SysConfigMapper, SysConfig> implements ConfigService {

    @Override
    public SysConfig getConfigByName(String name) {
        return getOne(new QueryWrapper<SysConfig>().lambda().eq(SysConfig::getConfName, name)
                .eq(SysConfig::getConfStatus, CommonStatusEnums.ACTIVE.getCode()).last("limit 1"));
    }

    @Override
    public ConfigRep getConfigRepByName(String name) {
        SysConfig config = this.getConfigByName(name);
        return ObjectUtils.isEmpty(config) ? null : ModelMapperUtil.map(config, ConfigRep.class);
    }

    @Override
    public List<SysConfig> getConfigByGroup(Integer group) {
        return list(new QueryWrapper<SysConfig>().lambda().eq(SysConfig::getConfGroup, group)
                .eq(SysConfig::getConfStatus, CommonStatusEnums.ACTIVE.getCode()));
    }

    @Override
    public List<ConfigRep> getConfigRepByGroup(Integer group) {
        List<SysConfig> configList = this.getConfigByGroup(group);
        if (ObjectUtils.isEmpty(configList)) {
            return null;
        }
        return configList.stream().map(item -> ModelMapperUtil.map(item, ConfigRep.class))
                .collect(Collectors.toList());
    }

    @Override
    public Map<String, SysConfig> getConfigMapByGroup(Integer group) {
        return getConfigByGroup(group).stream()
                .collect(Collectors.toMap(SysConfig::getConfName, item -> item));
    }

    @Override
    public Map<String, String> getConfig2MapByGroup(Integer group) {
        return getConfigByGroup(group).stream().collect(Collectors.toMap(SysConfig::getConfName, SysConfig::getConfValue));
    }

    @Override
    public Map<String, String> getConfig2MapByGroup(Integer group, String filterConfig) {
        return getConfigByGroup(group).stream()
                .filter(item -> !StringUtils.equals(item.getConfName(), filterConfig))
                .collect(Collectors.toMap(SysConfig::getConfName, SysConfig::getConfValue));
    }

    @Override
    public Boolean configValueIsValid(String name) {
        SysConfig config = this.getConfigByName(name);
        if (ObjectUtils.isEmpty(config)) {
            return false;
        }
        return StringUtils.equals(config.getConfValue(), ConfigConstants.Value.ACTIVE.getCode());
    }

    @Override
    public SysConfig getByGroupAndName(Integer group, String name) {
        Map<String, Object> param = Maps.newHashMap();
        if(!ObjectUtils.isEmpty(group)){
            param.put("conf_group", group);
        }
        param.put("conf_name", name);
        param.put("conf_status", 1);
        List<SysConfig> configList = baseMapper.selectByMap(param);
        if(!ObjectUtils.isEmpty(configList)){
            return configList.get(0);
        }
        return null;
    }

    @Override
    public SysConfig getEthNodeConfig(){
        return getByGroupAndName(ConfigConstants.Group.wallet.getCode(),ConfigConstants.ETH_NODE_RPC);
    }

    @Override
    public SysConfig getByName(String name) {
        return getByGroupAndName(null, name);
    }
}
